package csc.fresher.view;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.SystemRecordLogin;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemUserLogout implements Serializable {
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemUserService systemUserService;

	@Autowired
	private SystemUserManageBean systemUserManageBean;

	@Autowired
	SystemLoginManageBean systemLoginManageBean;

	public static boolean hasLogout = false;

	public void logout() {

		SystemRecordLogin systemRecordLogin = new SystemRecordLogin();
		systemRecordLogin.setBeginTime(SystemLoginManageBean.beginTime);
		systemRecordLogin.setSystemUserID(systemUserService.getSystemUserIDByUserName(systemLoginManageBean.getUserName()));		
				
		systemRecordLogin.setEndTime(new Timestamp(System.currentTimeMillis()));

		systemUserService.createRecordLogin(systemRecordLogin);
		systemLoginManageBean.setSystemUser(null);
		hasLogout = true;
		FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
	}
}
